package com.group.three.controller.orderController;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * * 个人订单页，评论/投诉/售后图片上传
 */
@RestController
@RequestMapping("/comment")
@Api(tags = "买家端,图片上传接口")
public class CommentUpload {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      @ApiOperation(value = "获取图片信息")
        @PostMapping("/imageUpload")
        public Map<String, Object> imageUploadEditor(MultipartFile file, HttpServletRequest req) throws IOException {
            Map<String, Object> result = new HashMap<>();
            if (file.isEmpty()) {
                result.put("errno", "error");
                result.put("message", "数据为空");
                return result;
            } else {

                if (ImageIO.read(file.getInputStream())==null) {
                    result.put("errno", "error");
                    result.put("message", "文件类型不对");
                    return result;
                }
                String format = sdf.format(new Date());
                //获取resource目录路径
                File directory = new File("mall_front_desk/src/main/resources");
                //自定义路径
                String reportPath = directory.getCanonicalPath()+ "/static/commentImage/" +format;
                System.out.println(reportPath);
                File folder = new File(reportPath);
                if (!folder.exists()) {
                    folder.mkdirs();
                }
                //文件名
                String originName = file.getOriginalFilename();
                // 获取原始文件名的后缀
                String suffix = originName.substring(originName.lastIndexOf("."));
                String newName = UUID.randomUUID().toString() + suffix;
                try {
                    file.transferTo(new File(folder, newName));
                    String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/static/commentImage/" + format + "/" + newName;
                    System.out.println(url);
                    result.put("errno", "success");
                    result.put("url", url);
                } catch (IOException e) {
                    result.put("errno", "error");
                    result.put("message", e.getMessage());
                    e.printStackTrace();
                }
            }
            return result;
        }

    /**
     * *前端图片回调取消后删除对应的图片
     * @param URL 前台图片回调地址
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "前端图片回调取消后删除对应的图片")
    @GetMapping("/imageCancelUpload")
    public String imageCancelUpload(String URL) throws IOException {
        System.out.println(URL);
        //获取resource目录路径
        File directory = new File("mall_front_desk/src/main/resources");
        //自定义路径
        String reportPath = directory.getCanonicalPath()+ "/static/commentImage/";
        String url = StringUtils.substringAfter(URL, "commentImage/");
        File file = new File(reportPath+url);
        System.out.println(file);
        file.delete();
        return "success";
    }


}
